import {Injectable} from '@angular/core';
import {Headers, RequestOptions} from '@angular/http';
import {adSystemApiUrl, appletApiUrl} from '../../../config/api';
import {HttpService} from '../../lib/service/http.service';

@Injectable()

export class ProductService {

  constructor(private httpService: HttpService) {
  }

  addGoods(
    shop_id,
    categories,
    name, // 商品名称
    numbers, // 产品编号
    price_discount,//微信价
    price_origin,//原价
    status,// '下架'  '上架'
    pics,
    content, // 描述
    is_recommend,
    for_sale,
    attribute_template_id,
    skus
  ) {
    const url = `${appletApiUrl}/backend/goods/create`;
    console.log('000skus', skus);
    let json = {
      shop_id: shop_id,
      // ringify(goods_bind_attribute),
      name: name,
      numbers: numbers,
      price_discount: price_discount,
      price_origin: price_origin,
      pics: pics,
      content: content,
      is_recommend: is_recommend,
      for_sale: for_sale,
      attribute_template_id: attribute_template_id,
      categories: categories,
      skus: skus
    };
    return this.httpService.httpPostAuth(url, json).map(rs => rs);
  }

  editGood(
    id: number,
    shop_id,
    categories,
    name, // 商品名称
    numbers, // 产品编号
    price_discount, //价
    price_origin, //原价
    status, // '下架'  '上架'
    pics,
    content, // 描述
    is_recommend,
    for_sale,
    attribute_template_id,
    skus) {
    const url = `${appletApiUrl}/backend/goods/update`;
    let json = {
      id: Number(id),
      shop_id: shop_id,
      // ringify(goods_bind_attribute),
      name: name,
      numbers: numbers,
      price_discount: price_discount,
      price_origin: price_origin,
      pics: pics,
      content: content,
      is_recommend: is_recommend,
      for_sale: for_sale,
      attribute_template_id: attribute_template_id,
      categories: categories,
      skus: skus,
    };
    return this.httpService.httpPostAuth(url, json).map(rs => rs);
  }

  getCustomSkuByGoodsId(goods_id) {
    const url = `${adSystemApiUrl}/v1/member/custom_sku/getCustomSkuByGoodsId?goods_id=${goods_id}`;
    return this.httpService.httpPostAuth(url, '').map(rs => rs)
  }

  getGoodsSkubindList(goods_id) {
    const url = `${adSystemApiUrl}/v1/member/goods_sku/bindList?goods_id=${goods_id}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  getAppletGoodsCategoryRelationBindList(goods_id) {
    const url = `${adSystemApiUrl}/v1/member/applet_goods_category_relation/bindList?goods_id=${goods_id}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  getExpressTemplateList(name = '', page = 1, pageSize = 999) {
    const url = `${adSystemApiUrl}/v1/member/express_template/list` +
      `?mame=${name}&page=${page}&pageSize=${pageSize}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  // 分类绑定自定义sku
  bindGoodsCategory(goods_category_id,
                    name,
                    data) {
    const url = `${adSystemApiUrl}/v1/member/custom_sku/bindGoodsCategory`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({
      goods_category_id: goods_category_id,
      name: name,
      data: JSON.stringify(data)
    });
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }
}
